package com.twetec.cms.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.twetec.cms.model.po.system.RolePO;
import com.twetec.cms.model.vo.system.RoleVO;
import com.twetec.cms.service.system.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.twetec.cms.dao.mapper.system.RoleMapper;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author zhxl
 * @since 2020-10-12
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RolePO> implements RoleService {

    @Override
    public RoleVO getByUnionRoleId(Long unionRoleId) {
        if (null == unionRoleId || unionRoleId <= 0){
            return null;
        }
        QueryWrapper<RolePO> queryWrapper = new QueryWrapper();
        queryWrapper.eq("UNION_ROLE_ID", unionRoleId);
        RolePO rolePO = this.getOne(queryWrapper);
        if (null == rolePO){
            return null;
        }
        RoleVO roleVO = new RoleVO();
        BeanUtils.copyProperties(rolePO, roleVO);
        return roleVO;
    }

    @Override
    public boolean create(Long unionRoleId, String name) {

        RolePO addRolePO = new RolePO();
        addRolePO.setName(name);
        addRolePO.setUnionRoleId(unionRoleId);
        boolean save = this.save(addRolePO);
        return save;
    }

    @Override
    public boolean updateByUnionRoleId(Long unionRoleId, String name) {


        UpdateWrapper<RolePO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("UNION_ROLE_ID", unionRoleId);
        updateWrapper.set("name", name);

        boolean result = this.update(updateWrapper);
        return result;
    }
}
